package weixinkeji.dev.power.center.h;

import java.util.ArrayList;
import java.util.List;
import sys.wx.c.ATR;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.ann.power.head.GL;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.m.db.dao.mysql.Dao2;
import sys.wx.util.ToolService;
import weixinkeji.dev.power.center.bean.SysUP;
import weixinkeji.dev.power.center.check.CheckSysPower;
import weixinkeji.dev.power.center.so.URPSO;

/**
 *
 * @author IK
 */
@H("weixinkeji/dev/power/center/sysup/adu")
@GL
public class SysUPADU {

    ATR a;                  //ATR是必须要的。它封装了web请求的相关信息
    URPSO so = new URPSO();
    private final CheckSysPower check = new CheckSysPower();

    public SysUPADU(ATR obj) {     //这个有参构造函数，也是必须的。它进行附值
        this.a = obj;
    }

    @M("/add")//weixinkeji/dev/power/center/sysup/adu/add.imy
    public String a1() {
        String uID = a.request.getParameter("uID");//进行附权的用户ID
        String pIDs = a.request.getParameter("pID");//新的权限集合。数据格式为"p1,p2,p3..."
        if (isError(pIDs)) {//查检权限的值是不是合法（禁止非正常权限值添加及过滤敏感权限
            return null;
        }
//三目运算符下。如果权限是空，则只清空用户拥有的权限。不为空，则清空原有权限。再重新分配新权限
        return ToolService.IsNullOREmpty(pIDs) ? clear(uID) : add(uID, pIDs);
    }
    private String clear(String uID) {
        a.out_OK_NO(Dao.executeUpdate(so.clearUserPower(uID)));
        return null;
    }
    private String add(String uID, String pIDs) {
        List<SysUP> lup = new ArrayList<>();
        SysUP obj;
        for (String pid : pIDs.split(",")) {
            obj = new SysUP();
            obj.setUser_id(uID);
            obj.setPower_id(pid);
            lup.add(obj);
        }
        a.out_OK_NO(Dao2.sql(so.clearUserPower(uID), so.setUP(lup)));
        return null;
    }

    private boolean isError(String pids) {
        if (null == pids) {
            return false;
        }
        for (String pid : pids.split(",")) {
            if (check.isSysPower(pid.trim())) {
                return true;
            }
        }
        return false;
    }
}
